<?php /* $Id: view.php,v 1.89.4.5 2006/11/22 19:35:14 gregorerhardt Exp $ */
$task_id = intval( dPgetParam( $_GET, "task_id", 0 ) );
$task_log_id = intval( dPgetParam( $_GET, "task_log_id", 0 ) );
$reminded = intval( dPgetParam( $_GET, 'reminded', 0) );


// check permissions for this record
$canRead = !getDenyRead( $m, $task_id );
$canEdit = !getDenyEdit( $m, $task_id );
// check permissions for this record
$canReadModule = !getDenyRead( $m );


if (!$canRead) {
	$AppUI->setMsg('Access denied', UI_MSG_ERROR);
	$AppUI->redirect();
}
$q =& new DBQuery;
$perms =& $AppUI->acl();

$q->addTable('tasks');
$q->leftJoin('users', 'u1', 'u1.user_id = task_owner');
$q->leftJoin('projects', 'p', 'p.project_id = task_project');
$q->leftJoin('task_log', 'tl', 'tl.task_log_task = task_id');
$q->addWhere('task_id = ' . $task_id);
$q->addQuery('tasks.*');
$q->addQuery('project_name, project_color_identifier, project_owner');
$q->addQuery('u1.user_username as username');
$q->addQuery('ROUND(SUM(task_log_hours),2) as log_hours_worked');
$q->addGroup('task_id');

// check if this record has dependencies to prevent deletion
$msg = '';
$obj = new CTask();
$canDelete = $obj->canDelete( $msg, $task_id );

//$obj = null;
$sql = $q->prepare();
$q->clear();

if (!db_loadObject( $sql, $obj, true, false )) {
	$AppUI->setMsg( 'Task' );
	$AppUI->setMsg( 'invalidID', UI_MSG_ERROR, true );
	$AppUI->redirect();
} else {
	$AppUI->savePlace();
}

if (!$obj->canAccess( $AppUI->user_id )) {
	$AppUI->setMsg('Access denied', UI_MSG_ERROR);
	$AppUI->redirect();
}

// Clear any reminders
if ($reminded)
	$obj->clearReminder();

// retrieve any state parameters
if (isset( $_GET['tab'] )) {
	$AppUI->setState( 'TaskLogVwTab', $_GET['tab'] );
}
$tab = $AppUI->getState( 'TaskLogVwTab' ) !== NULL ? $AppUI->getState( 'TaskLogVwTab' ) : 0;

// get the prefered date format
$df = $AppUI->getPref('SHDATEFORMAT');
//Also view the time
//$df .= ' ' . $AppUI->getPref('TIMEFORMAT');

$start_date = intval( $obj->task_start_date ) ? new CDate( $obj->task_start_date ) : null;
$end_date = intval( $obj->task_end_date ) ? new CDate( $obj->task_end_date ) : null;

//check permissions for the associated project
$canReadProject = !getDenyRead( 'projects', $obj->task_project);

// get the users on this task
$q->addTable('users', 'u');
$q->addTable('user_tasks', 't');
$q->leftJoin('contacts', 'c' , 'user_contact = contact_id');
$q->addQuery('u.user_id, u.user_username, contact_email, t.perc_assignment');
$q->addQuery('CONCAT(c.contact_first_name, " ", c.contact_last_name) AS contact_name');
$q->addWhere('t.task_id = ' . $task_id);
$q->addWhere('t.user_id = u.user_id');
$q->addOrder('u.user_username');
$users = $q->loadList();

$efforts = 0.0;
foreach ($users as $u) {
	$efforts += $obj->task_duration * $u['perc_assignment'] / 100;
}
$isMyProject = ($AppUI->user_id == $obj->project_owner);
$isMyTask = $isMyProject || ($AppUI->user_id == $obj->task_owner);

if (!$isMyTask) {
	foreach ($users as $u) {
		if ($u['user_id'] == $AppUI->user_id) {
			$canAddSubTask = true;
			break;
		}
	}
}
$durnTypes = dPgetSysVal( 'TaskDurationType' );

// setup the title block
$titleBlock = new CTitleBlock( 'View Task' );
$titleBlock->addButton( $AppUI->_('tasks list'), 'index.php?m=tasks&project_id='.$obj->task_project );
if ($canEdit) {
	if ($isMyTask || $canAddSubTask) {
		$titleBlock->addButton( $AppUI->_('add sub task'),
			'index.php?m=tasks&a=simpleaddedit&task_project='.$obj->task_project.'&task_parent='.$task_id);
	}
}
$titleBlock->addCell();
$titleBlock->show();

$task_types = dPgetSysVal('TaskType');

?>

<script language="JavaScript">
<? if ($canReadProject) { ?>
function viewProject() {
	go('?m=projects&a=view&project_id=<?=$obj->task_project?>');
}
<? } else { ?>
function viewProject() {}
<? }

if ($canEdit) {
?>

function updateTask() {
	var f = document.editFrm;
	if (f.task_log_description.value.length < 1) {
		alert( "<?=$AppUI->_('tasksComment', UI_OUTPUT_JS)?>" );
		f.task_log_description.focus();
	} else if (isNaN( parseInt( f.task_percent_complete.value+0 ) )) {
		alert( "<?=$AppUI->_('tasksPercent', UI_OUTPUT_JS)?>" );
		f.task_percent_complete.focus();
	} else if(f.task_percent_complete.value  < 0 || f.task_percent_complete.value > 100) {
		alert( "<?=$AppUI->_('tasksPercentValue', UI_OUTPUT_JS)?>" );
		f.task_percent_complete.focus();
	} else {
		f.submit();
	}
}

function doTaskEdit() {
	go('index.php?m=tasks&a=simpleaddedit&task_id=<?=$task_id?>');
}

function doTaskDelete() {
	if (confirm( "<?=$AppUI->_('doDelete', UI_OUTPUT_JS).' '.$AppUI->_('Task', UI_OUTPUT_JS).'?'?>" )) {
		document.frmDelete.submit();
	}
}

function doTaskComplete(cell) {
	ajax.send('index.php?m=tasks&a=complete&suppressHeaders=1&task_id=<?=$task_id?>', '',
		function(r) {
			if (r != 'OK') {
				alert(r);
				return;
			}
			Control(cell).hide();
			$('idPercent').innerHTML = '<blink>100%</blink>';
		}
	);
}
<? } else { ?>
function doTaskEdit() {}
function doTaskDelete() {}
<? } ?>
</script>

<form name="frmDelete" action="index.php?m=tasks" method="post">
	<input type="hidden" name="dosql" value="do_task_aed">
	<input type="hidden" name="del" value="1" />
	<input type="hidden" name="task_id" value="<?=$task_id?>" />
</form>

<table border="0" cellpadding="4" cellspacing="0" width="100%" class="smallinfopanel">
<tr>
	<th colspan="100%" style="cursor: default" class="last">
		<table border="0" cellpadding="0" cellspacing="1" width="100%">
			<tr>
				<td><img src="images/<?=$obj->task_milestone ? 'milestone.gif' : 'task.gif'?>" border="0"
						title="<?=$obj->task_milestone ? 'Milestone' : 'Task'?>"/></td>
				<td width="100%" style="color: white">&nbsp;
					<?php
						echo '<span onclick="viewProject()" class="clickable" title="View project">'.
								'[ '.@$obj->project_name.' ]'.
							 '</span> ';
						echo @$obj->task_name;
					?>
				</td>
			<? if ($canEdit && $isMyTask) { ?>
				<? if ($obj->task_percent_complete < 100) { ?>
				<td onclick="doTaskComplete(this)" class="clickable">
					<img src="images/force_complete.gif" border="0" title="Force complete"/>
				</td>
				<? } ?>
				<td onclick="doTaskEdit()" style="padding-left: 4px" class="clickable">
					<img src="images/edit_task.gif" border="0" title="Edit task"/>
				</td>
			<? } ?>
			<? if ($canDelete && $isMyTask) { ?>
				<td onclick="doTaskDelete()" style="padding-left: 4px" class="clickable">
					<img src="images/delete_task.gif" border="0" title="Delete task"/>
				</td>
			<? } ?>
			</tr>
		</table>
	</th>
</tr>
<tr valign="top">
	<td width="50%">
		<table width="100%" cellspacing="0" cellpadding="3" border="0" class="list">
		<tr class="info">
			<td nowrap="nowrap" colspan="4"><?=$AppUI->_('Details')?></td>
		</tr>
		<!--tr class="second">
			<td align="right" nowrap="nowrap"><?=$AppUI->_('Color')?>:</td>
			<td>
				<span style="background-color:#<?=$obj->project_color_identifier?>; border: 1px solid black">
					&nbsp;&nbsp;&nbsp;&nbsp;
				</span>
			</td>
		</tr-->
		<?php if ( $obj->task_parent != $obj->task_id ) {
			$obj_parent = new CTask();
			$obj_parent->load($obj->task_parent);
		?>
		<tr class="second">
			<td align="right" nowrap="nowrap"><?=$AppUI->_('Task Parent')?></td>
			<td colspan="3"><a href="<?='index.php?m=tasks&a=view&task_id='.@$obj_parent->task_id?>"><?=@$obj_parent->task_name?></a></td>
		</tr>
		<?php } ?>
		<tr class="second">
			<td align="right" nowrap="nowrap"><?=$AppUI->_('Priority')?></td>
			<td><?php $task_priotities = dPgetSysVal('TaskPriority');
					echo ucfirst($AppUI->_($task_priotities[$obj->task_priority]));?>
			</td>
			<td align="right" nowrap="nowrap"><?=$AppUI->_('Creator')?></td>
			<td> <?=ucfirst(@$obj->username)?></td>
		</tr>
		<tr class="second" style="display: none">
			<td align="right" nowrap="nowrap"><?=$AppUI->_('Milestone')?></td>
			<td><?$obj->task_milestone ? $AppUI->_('Yes') : $AppUI->_('No')?></td>
			<td align="right" nowrap="nowrap"><?=$AppUI->_('Type')?></td>
			<td><?=$AppUI->_($task_types[$obj->task_type])?></td>
		</tr>
		<tr class="second" style="display: none">
			<td align="right"><?=$AppUI->_('Web')?></td>
			<td colspan="3"><a href="<?=@$obj->task_related_url?>" target="task<?=$task_id?>"><?=@$obj->task_related_url?></a></td>
		</tr>
		<tr class="second">
			<td align="right" valign="top"><?=$AppUI->_('Description')?></td>
			<td colspan="3" style="height: 48px"  valign="top">
				<?=str_replace( chr(10), '<br/>', $obj->task_description)?>
			</td>
		</tr>
		<tr class="info">
			<td nowrap="nowrap" colspan="2">
				<?=$AppUI->_('Progression').':&nbsp;&nbsp;<span id="idPercent">'.@$obj->task_percent_complete.'%</span>'?>
			</td>
			<td nowrap="nowrap" colspan="2">
				<?=$AppUI->_('Time Worked').': '.(@$obj->task_hours_worked + @rtrim($obj->log_hours_worked, '0')).' '.$AppUI->_( $durnTypes[$obj->task_duration_type] )?>
			</td>
		</tr>
		<tr class="second">
			<td align="right" nowrap="nowrap"><?=$AppUI->_('Start Date')?></td>
			<td><?=$start_date ? $start_date->format( $df ) : '-'?></td>
			<td align="right" nowrap="nowrap"><?=$AppUI->_('Finish Date')?></td>
			<td><?=$end_date ? $end_date->format( $df ) : '-'?></td>
		</tr>
		<tr class="second">
			<td align="right" nowrap="nowrap" valign="top"><?=$AppUI->_('Expected Duration')?></td>
			<td><?=sprintf('%.2f', $obj->task_duration).' '.$AppUI->_( $durnTypes[$obj->task_duration_type] )?></td>
			<td align="right" nowrap="nowrap"><?=$AppUI->_('Expected Efforts:')?></td>
			<td><?=sprintf('%.2f', $efforts).' '.$AppUI->_( $durnTypes[$obj->task_duration_type] )?></td>
		</tr>
		</table>
	</td>

	<td width="50%">
		<table cellspacing="0" cellpadding="3" border="0" width="100%" class="list">
		<tr class="info">
			<td><?=$AppUI->_('Assigned Users')?></td>
		</tr>
		<?php
			echo count( $users ) == 0 ? '<tr class="second"><td>&nbsp;</td></tr>' : '';
			foreach($users as $row) {
		?>
		<tr class="second">
			<td style="padding-left: 8px">
				<div style="float: left">
				<img src="images/mail.gif" border="0">&nbsp;
				<a href="mailto:<?=$row['contact_email']?>"><?=$row['contact_name']?> &lt;<?=$row['contact_email']?>&gt;</a>
				</div>
				<div style="float: right"><?=$row['perc_assignment']?>%&nbsp;</div>
			</td>
		</tr>
		<?
				if ($row['user_id'] == $AppUI->user_id) {
					$isMyTask = true;
				}
			}
		?>

		<?php
			// Pull tasks dependencies
			$q->addQuery('td.dependencies_req_task_id, t.task_name');
			$q->addTable('tasks', 't');
			$q->addTable('task_dependencies', 'td');
			$q->addWhere('td.dependencies_req_task_id = t.task_id');
			$q->addWhere('td.dependencies_task_id = ' . $task_id);

			$taskDep = $q->loadHashList();
			$q->clear();
		?>
		<tr class="info">
			<td><?=$AppUI->_('Predecessors')?></td>
		</tr>
		<?php
			echo count( $taskDep ) == 0 ? '<tr class="second"><td>&nbsp;</td></tr>' : '';
			foreach ($taskDep as $key => $value) {
				echo '<tr class="second">'.
					'<td style="padding-left: 8px"><img src="images/task_rel.gif" border="0">&nbsp;' .
					'<a href="./index.php?m=tasks&a=view&task_id='.$key.'">'.$value.'</a>' .
					'</td></tr>';
			}
		?>
        <?php
			// Pull the tasks depending on this Task
			$q->addQuery('td.dependencies_task_id, t.task_name');
			$q->addTable('tasks', 't');
			$q->addTable('task_dependencies', 'td');
			$q->addWhere('td.dependencies_task_id = t.task_id');
			$q->addWhere('td.dependencies_req_task_id = ' . $task_id);
			$dependingTasks = $q->loadHashList();
			$q->clear();
		?>
		<tr class="info">
			<td><b><?=$AppUI->_('Followers')?></b></td>
		</tr>
		<?php
			echo count( $dependingTasks ) == 0 ? '<tr class="second"><td>&nbsp;</td></tr>' : '';
			foreach($dependingTasks as $key => $value) {
				echo '<tr class="second">'.
					'<td style="padding-left: 8px"><img src="images/task_rel.gif" border="0">&nbsp;' .
					'<a href="index.php?m=tasks&a=view&task_id='.$key.'">'.$value.'</a>' .
					'</td></tr>';
			}

		/*
		 	require_once  $AppUI->getSystemClass( 'CustomFields' );
			$custom_fields = New CustomFields( $m, $a, $obj->task_id, "view" );
			$custom_fields->printHTML();
		*/
		?>
		</table>
	</td>
</tr>
</table>

<?php
$tabBox = new CTabBox( "?m=tasks&a=view&task_id=$task_id", $tab );

$tabBox_show = 0;
if ( $obj->task_dynamic != 1 ) {
	// tabbed information boxes
	if ($perms->checkModuleItem('task_log', 'view', $task_id)) {
		$tabBox_show = 1;
		$tabBox->add( 'vw_logs', 'Task Logs' );
		// fixed bug that dP automatically jumped to access denied if user does not
		// have read-write permissions on task_id and this tab is opened by default (session_vars)
		// only if user has r-w perms on this task, new or edit log is beign showed
		if ($isMyTask) {
	        if ($task_log_id == 0) {
	            if ($perms->checkModuleItem('task_log', 'add', $task_id))
	                $tabBox->add( 'vw_log_update', 'New Log' );
	        }
	        else if ($perms->checkModuleItem('task_log', 'edit', $task_id)) {
	            $tabBox->add( 'vw_log_update', 'Edit Log' );
			}
		}
	}
}

if ( count($obj->getChildren()) > 0 ) {
	// Has children
	// settings for tasks
	$f = 'children';
	$min_view = true;
	$tabBox_show = 1;
	// in the tasks file there is an if that checks
	// $_GET[task_status]; this patch is to be able to see
	// child tasks withing an inactive task
	$_GET['task_status'] = $obj->task_status;
	$tabBox->add( 'tasks', 'Child Tasks' );
}

$tabBox->add( 'file', 'Files' );

if ($isMyTask && $tabBox->loadExtras($m, $a))
	$tabBox_show = 1;

if ( $tabBox_show == 1)	$tabBox->show();
?>
